<?php

class ChargesController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	} 
	//查询学生列表	
	 public function indexAction() {
		try {  	
				  
		    $where='where  1=1';	
			 
		   $groupid = ( int ) $this->_request->getParam ( 'groups' );
		   $school_id = $this->_request->getParam ( 'school' ); 		
	       $classid  = $this->_request->getParam ( 'class' ); 
		   $cnname  = $this->_request->getParam ( 'cnname' ); 
		    
		  $this->view->groups=$groupid; 
		  $this->view->schoolid=$schoolid;  
		  $this->view->classid=$classid; 
		  $this->view->cnname=$cnname; 
		  
				 if($school_id){ 
					 $schoolarray=explode('_',$school_id);  
					  $schoolid=$schoolarray['1'];
				}
				   
				 if($groupid){
					  $where .=' and  groupid="'.$groupid.'" ';
					 $this->view->groupid= $groupid; 
					 }
					 
				 if($schoolid){
					$where .=' and  schoolid="'.$schoolid.'" '; 
					$this->view->schoolid= $school_id; 
					} 
					
				 if($classid){
					$where .=' and  classid="'.$classid.'" ';
					  $this->view->classid= $classid; 
					 }	
					 
				 if($cnname){
					$where.=' and cnname="'.trim($cnname).'" ';
					$this->view->cnname= $cnname; 				 
					}else{
					 //throw new Exception ( '请输入您所要查询的孩子名称' );	 
					} 
			  
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS s.cnname,p.addtime,p.clientip,p.paytype,p.pid,p.memo,p.accountid,p.orderid,p.amount  from   pay_account p  left join  student s on  s.uid = p.uid_student     ".$where."  order by p.addtime DESC limit $offset,$count";
			    //echo $sql;  exit;
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			      // print_r($result);
		    	$this->view->result= $result;
			  
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid,'class'=>$classid) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	   
	//查询所有的家长信息
	
   public function parentsAction() {
		try { 
		    
		    $where='';	
			 
		   $keywolrd =  $this->_request->getParam ( 'text' ); 
		   $this->view->keywolrd=$keywolrd; 
			
			
		    if($keywolrd){
			  $where.=' where  username like "%'.$keywolrd.'%" or mobile like "%'.$keywolrd.'%" or realname  like "%'.$keywolrd.'%" ';	
			   } 
			$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			$this->view->pageNum=$curpage;		
    			$offset = ($curpage - 1) * $count; 
			 $sql="select SQL_CALC_FOUND_ROWS *  from parents ".$where."  order by pid ASC limit $offset,$count";
			// echo $sql;  exit;
			$result = $this->_dbAdapter->fetchAll ($sql);  
			  foreach($result as $key => $value){
				  
			 	  $result[$key]['password']= $this->authcode($value['password'], 'DECODE', 'mactop') ;
				                          
		       }  
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}  
	      


   public function groupsAction() {
		try { 
		 $p =$this->_request->getPost ();
         $sql="select * from  groups ";
	     $result = $this->_dbAdapter->fetchAll ($sql);
		 
		 $str.='<option value="0" >==请选择机构==</option>';
		 for($i = 0; $i<count($result); $i++)
		{
			$str .= "<option";
			if($p['jigouid']==$result[$i]['groupid']){
				$str .=" selected=selected ";
			}
	      $str .= " value='".$result[$i]['groupid']."'>".$result[$i]['groupname']."</option>";
		}
		  
	    echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//获取所有的校园列表
	  public function schoollistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		  $p =$this->_request->getPost (); 
		   
		  $array=explode('_',$p['sid']);
		  
		  $sql="SELECT * FROM `groups` where groupid='".$p['id']."' ";
		  $result = $this->_dbAdapter->fetchRow ($sql);
	          
/*			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
*/			 
		    $sql="select * from  school where groupid='".$p['id']."' ";   
			$schoollist=$this->_dbAdapter->fetchAll ($sql);
	                 
            $str.='<option value="0" >==请选择学校==</option>';
			// echo $p['id'];   exit;
		 for($i = 0; $i<count($schoollist); $i++)
		{
		  $str .= "<option ";
		  
		  if ($array['1']==$schoollist[$i]['schoolid']){
		      $str .=" selected=selected";
		  }
		   $str .= " value='".$p['id'].'_'.$schoollist[$i]['schoolid']."'>";
		   $str .= $schoollist[$i]['schoolname']."</option>";
		   
		}
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	//获取所有的班级信息
	  public function classlistAction() {
		try { 
           if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
		    $p =$this->_request->getPost ();  
		    $array=explode('_',$p['id']); 
			 
		    $sql="SELECT * FROM `groups` where groupid='".$array['0']."' ";
		 // $sql="SELECT * FROM `groups` where groupid='".$p['gid']."' ";
		    $result = $this->_dbAdapter->fetchRow ($sql);
	      
		 
/*			//连接运营库
			$dbArray=array();
			$dbArray['host']=$result['dbhost'];
			$dbArray['username']=$result['dbusername'];
			$dbArray['password']=$this->authcode($result['dbpassword'], 'DECODE', 'mactop') ;
			$dbArray['dbname']=$result['dbname'];
			
			$dbAdapter = Zend_Db::factory ("PDO_MYSQL", $dbArray );
			Zend_Registry::set('dbAdapter', $dbAdapter);

			$dbAdapter=Zend_Registry::get('dbAdapter');
			$dbAdapter->query ( 'set names utf8');	
*/			 
		  $sql="select * from  class  where isdel=0 and schoolid='".$array[1]."' ";   
		  $classlist= $this->_dbAdapter->fetchAll ($sql);
	      $str.='<option value="0" >==请选择班级==</option>';
		  	 
		 for($i = 0; $i<count($classlist); $i++)
		{
		  $str .= "<option ";
		  if($p['cid']==$classlist[$i]['classid']){
		     $str .=" selected=selected";
		  } 
		  $str .= "  value='".$classlist[$i]['classid']."'>".$classlist[$i]['classname']."</option>";
		    
		} 
		   
	      echo $str;  exit;
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	 public function addAction() {
		try { 
		 
		  //查询settin 表中的基础服务和相册、课件的价格
			$sql="select  * from setting ";
			$setresult = $this->_dbAdapter->fetchAll ($sql); 
			foreach($setresult  as $key => $value){
				if($value['variable']=='base'){
					$base= $value['value'];
				 } else if($value['variable']=='courseware'){
					$courseware= $value['value'];  
				 }else if($value['variable']=='album'){
					$album= $value['value'];  
				 }
			}
		  	
		 $this->view->base=$base;	
		 $this->view->courseware=$courseware;	
		 $this->view->album=$album;	
		//	print_r($setresult);
		
	      //查询学生的信息 
		   $where='';	
		   $result=array();
		   $keyworld =  trim($this->_request->getParam ( 'keyworld' ));  
		   if($keyworld){
			    $this->view->keyworld=$keyworld;	
		     $where .='and (cnname like "%'.$keyworld.'%" or mobile like "%'.$keyworld.'%" or username like "%'.$keyworld.'%")';           $sql="select uid,pid,cnname,nikename,sex,birthday,realname,mobile,username,isinstalled,groupname,schoolname,classname from group_student_class_parents_v where 1 ".$where;  
			$studentlist = $this->_dbAdapter->fetchAll ($sql);  
		   } 
		  
		    if($studentlist){ 
				 	foreach($studentlist as $key => $value){
						if(!isset($result[$value['pid']])){
							$uidlist[]=$value['uid'];
							$result[$value['pid']]['parent']['realname']=stripslashes($value['realname']);
							$result[$value['pid']]['parent']['pid']=$value['pid'];
							$result[$value['pid']]['parent']['mobile']=$value['mobile'];
							$result[$value['pid']]['parent']['username']=stripslashes($value['username']);
							$result[$value['pid']]['parent']['ischeck_mobile']=$value['ischeck_mobile'];
							$result[$value['pid']]['parent']['isinstalled']=$value['isinstalled'];
						}
						if(!isset($result[$value['pid']]['student'][$value['uid']])){
							$result[$value['pid']]['student'][$value['uid']]=array(
								'uid'=>$value['uid'],
								'cnname'=>stripslashes($value['cnname']),
								'nikename'=>stripslashes($value['nikename']),
								'sex'=>$value['sex'],
								'sexDisp'=>$value['sex']==1?'男':'女',
								'birthday'=>$value['birthday'],
								'age'=>$this->getAge($value['birthday']),
							);
						}

						$result[$value['pid']]['student'][$value['uid']]['class'][]=array(
								'groupname'=>$value['groupname'],
								'schoolname'=>$value['schoolname'],
								'classname'=>$value['classname'],
						);
					}
					
				} 
			if($uidlist){
				  $sql='select  * from pay_account where uid_student in ('.implode(',',$uidlist).')  order by uid_student'; 
				  $studentamount = $this->_dbAdapter->fetchAll ($sql);  				  
				 }
				// print_r($studentamount);
			if($studentamount){
				 foreach($studentamount as $key => $value){
					 if($value['paytype']=='add'){
					    $amount[$value['uid_student']]+=$value['amount'];
						 }else if($value['paytype']=='cost' or $value['pay_type']=='refund'){
							  $amount[$value['uid_student']]-=$value['amount']; 
						   } 
				     }
				}
			
			
			 
			 $this->view->amount=$amount;		  
		     $this->view->result=$result;	
	  
	   } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//账户充值金额操作
	 public function opaddAction() {
		try { 
	  
	    if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
	   $p =$this->_request->getPost ();  
	   
	   $amount=array();
	   $t=time(); 
	   $amount=array(
			'pid'=>$p['pid'],
			'uid_student'=>$p['uid'],
			'paytype'=>'add',
			'amount'=>$p['ammount'],
			'addtime'=>$t,
			'clientip'=>$this->getRequest ()->getClientIp(),
			   );  
	   $amountresult = $this->_dbAdapter->insert ('pay_account',$amount);  
	  if( $amountresult ){
		   $this->feedback ( '增加成功', '注意', '/'.$this->controller.'/operation/uid/'.$p['uid'].'/pid/'.$p['pid'].'/keyworld/'.$p['keyworld'], 'warning' );
	         }else{
				throw new Exception ( '增加失败 ！' );
    	    }
		
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//购买服务操作
	public function opcostAction() {
		try { 
	    
	   if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
	   $p =$this->_request->getPost ();  
	   //print_r($p); exit;
	  //Array ( [uid_student] => 49442 [uid_parent] => 49442 [keycnname] => 安宁 [balance] => [box] => 0.00 [productid] => 3 [effecttime] => [discount] => [totalprice] => 0.00 [trueprice] => 0.00 [memo] => ) 
	  $sql="select * from pay_product where productid='".$p['productid']."'";
	  $result = $this->_dbAdapter->fetchRow ($sql);
	   
	     //先插入订单表 
	   $orderarr=array(); 
	         if($p['discount']){
				  $discount=$p['discount'];
				}else{
					$discount='10.0';
				}   
   
		      $t=time();   
			    
	         $p['effecttime']= strtotime($p['effecttime']);  
			   
			 $orderarr=array(
				'ordertype'=>$result['producttype'],
				 'ordertitle'=>'购买服务'.$p['type'],
				'uid_student'=>$p['uid_student'],
				'paynum'=>'1', 
				'starttime'=>$result['starttime'],
				'endtime'=>$result['endtime'],
				'effecttime'=>$p['effecttime'],
				'price'=>$p['box'], 
				'addtime'=>$t,
				'memo'=>$p['memo'],
				'discount'=>$discount,
				'totalprice'=>$p['totalprice'],
				'trueprice'=>$p['trueprice'],
				'productid'=>$p['productid']
				   );  
		   $orderarr = $this->_dbAdapter->insert ('pay_order',$orderarr);   	  
		   $orderid=$this->_dbAdapter->lastInsertId(); 
		   if($orderid){
			   //$sql="select pid from student where uid=".$p['uid_student'];
			  // $pid = $this->_dbAdapter->fetchOne ($sql);   
			   if($orderid!=''){ 
				   $amount=array(
						'pid'=>$p['uid_parent'],
						'uid_student'=>$p['uid_student'],
						'orderid'=>$orderid,
						'paytype'=>'cost',
						'amount'=>$p['trueprice'],
						'addtime'=>$t,
						'clientip'=>$this->getRequest ()->getClientIp(),
						   );  
				  $amountresult = $this->_dbAdapter->insert ('pay_account',$amount);   
			   }
		 }
	if($amountresult){
		  $this->feedback ( '成功', '注意', '/'.$this->controller.'/operation/uid/'.$p['uid_student'].'/pid/'.$p['uid_parent'].'/keyworld/'.$p['keycnname'], 'tip',0); 		 
		}else{
		   throw new Exception ( '操作失败！' );
		}  
	 
	 } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//订单作废
	 public function deleteAction() {
		try { 
	   $cid =   $this->_request->getParam ( 'cid' );  
	   $id =   $this->_request->getParam ( 'id' ); 
	    
		if(empty($cid)){
			   throw new Exception ( '参数错误！' ); 
				} 
				
			if(empty($id)){
			   throw new Exception ( '参数错误！' ); 
				} 	
				
	  $res=$this->_dbAdapter->update('coupon_code_codes',array('is_available'=>1),"id='".$id."'");
	  if($res){
		  $this->feedback ( '已作废', '注意', '/'.$this->controller.'/useddetail/id/'.$cid, 'tip',0); 		 
		}else{
		   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}  
	
	
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 
	   public function accountdetailAction() {
			try { 
		    
			$accountid =   $this->_request->getParam ( 'id' );  
			if(empty($accountid)){
			   throw new Exception ( '参数错误！' ); 
				}  
			$name =   $this->_request->getParam ( 'name' ); 
			
		    $sql="select * from  pay_account where accountid=".$accountid;
		    $result = $this->_dbAdapter->fetchRow ($sql);   
		  
		    $this->view->result=$result;  
			$this->view->name=$name;  
		} catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
    //优惠券列表	
	
	public function couponlistAction() {
		try { 
		
		//优惠券类型
	     $sql="select * from  coupon_code  ";
	     $result = $this->_dbAdapter->fetchAssoc ($sql); 
		 //优惠券券号
		$sql="select  c_id,count(case when user_id<>0  and  fetch_time<>0 then 'check' end) a1,count(case when fetch_time=0 and user_id=0  then 'checked' end) a2   from coupon_code_codes group by c_id";   
	    $codesresult = $this->_dbAdapter->fetchAssoc ($sql);
	     
		$nowtime=date('Y-m-d',time()); 
		 foreach($result as $key => $value){
			 //抵佣金
			  if($value['c_type']=='gold'){
				  if(!empty($value['money_max']) && !empty($value['money_reduce'])){
					 $result[$key]['q_title']= $value['title'].'满'.$value['money_max'].'减'.$value['money_reduce'].'元优惠券';					                     $result[$key]['q_content']='满'.$value['money_max'].'减'.$value['money_reduce'];
					 $result[$key]['q_type']='优惠券';
					} else{
					  $result[$key]['q_title']= $value['title'].$value['price'].'元代金券'; 
					  $result[$key]['q_content']= $value['price'].'元代金券';	
					   $result[$key]['q_type']='代金券';
						}
			   }else if($value['c_type']=='discount'){
			   	//折扣	 
				$result[$key]['q_title']= $value['title'].$value['price'].'%折扣券'; 	 
				$result[$key]['q_content']= $value['price'].'%';
				$result[$key]['q_type']='折扣券';
			  } 
			    
			  //添加 使用 和 未使用的信息
			   $result[$key]['used']=$codesresult[$key]['a1'];
			   $result[$key]['unused']=$codesresult[$key]['a2']; 
			   
			   
		 }
		  
	     $this->view->result=$result;   
		 
	
	   } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
	} 
	// 查看谁使用了优惠券
	 public function useddetailAction() {
	   try {  
		 $id  = $this->_request->getParam ( 'id' );
		 $this->view->id=$id;   
	     if( empty($id) ){
			// throw new Exception ( '参数错误！' );  
			 }
		   $p =$this->_request->getPost ();  
		  
		   //  print_r($p);	  exit;
		  $where ='';
		   if(($p['used'] > 0) && ($p['unused'] > 0)){ 
		     $where.='   where c.c_id="'.$id.'" ';
			$this->view->used='1';  
			$this->view->unused='1'; 	 
		 } 
		  
		  if(empty($p['used']) && empty($p['unused'])){ 
		     $where.='   where c.c_id="'.$id.'" ';
			$this->view->used='1';  
			$this->view->unused='1'; 	 
		 } 
		   
		if($p['unused'] > 0 && empty($p['used']) ){
		      $where.="  where c.c_id='".$id."' and c.user_id <>0 and fetch_time <>0";
			  $this->view->unused='1';  	 
		  }
		  
		 if($p['used'] > 0 && empty($p['unused']) ){
			$where.="  where c.c_id='".$id."' and c.user_id=0 and fetch_time=0";
			 $this->view->used='1';  		
		  } 
			
			 
	     $sql="select * from  coupon_code_codes  c left join  group_school_class_student_v s on c.user_id=s.uid_student ".$where;
		 // echo $sql;  exit;
	     $couponresult = $this->_dbAdapter->fetchAll ($sql); 
	      // print_r($couponresult);
		 $this->view->result=$couponresult;    
	
	  } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
	} 
	 
	//优惠券充值
	
	  public function opyhqAction() {
			try { 
	
	       if (! $this->isPost ()) {
			    throw new Exception ( '参数错误！' );
			}			
	        $p =$this->_request->getPost ();  
			if(empty($p)){
				//参数错误
				echo "0";  exit; 
			} 
			$sql="select *  from coupon_code_codes s left join coupon_code c on c.c_id=s.c_id where s.code='".$p['code']."' and c.product_type like '%free%' and s.user_id=0 and is_available=1 ";  
			
			$result = $this->_dbAdapter->fetchRow ($sql);   
			if(!empty($result)){
				   echo '1';  exit; 
			   }else{
				  echo '3';  exit;	
			  }  
			 
			if($result['user_id']==0){
			   echo '1';  exit;
			   //可用
			 } else{
				 //不可用
			   echo "2";  exit; 
				 }
	       } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
	 
	 
	   public function opaddyhqAction() {
			try { 
	       //处理优惠券充值
	     if (! $this->isPost ()) {
			    throw new Exception ( '参数错误！' );
			}			
	        $p =$this->_request->getPost ();
			  
			 $p['code']=trim($p['code']);
			  
	        //更改优惠券的使用者
             $result = $this->_dbAdapter->update ('coupon_code_codes',array('user_id'=>$p['suid'],'fetch_time'=>time()),'code="'.$p['code'].'"'); 
		  
		    //给账户充值
			if($result){
				
			$sql="select *  from coupon_code_codes s left join coupon_code c on c.c_id=s.c_id where s.code='".$p['code']."'  "; 
			$coderesult = $this->_dbAdapter->fetchRow ($sql);  
			 
			if($coderesult){
				 
				  $amount=array();
				   $t=time();
				   $amount=array(
						'pid'=>$p['pid'],
						'uid_student'=>$p['suid'],
						'paytype'=>'add',
						'amount'=>$coderesult['price'],
						'addtime'=>$t,
						'clientip'=>$this->getRequest ()->getClientIp(),
						   );  
				   $amountresult = $this->_dbAdapter->insert ('pay_account',$amount);  
					if($amountresult){
					  $this->feedback ( '添加成功', '注意', '/'.$this->controller.'/operation/uid/'.$p['suid'].'/pid/'.$p['parent_id'].'/keyworld/'.$p['key'], 'tip',0 ); 	                  }else{
						$this->feedback ( '操作失败', '注意', '/'.$this->controller.'/operation/keyworld/'.$p['key'], 'warning' );  
					 } 
				  } 
			}
			
			
			//返回充值页面
	  
	  } catch ( Exception $e ) {
				//$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
				$this->feedback ( '操作失败', '注意', '/'.$this->controller.'/add?keyworld='.$p['key'], 'warning' );
			}
		} 
	 
	 
	   public function orderlistAction() {
			try { 
	   
	       $where='where p.isdelete=0 ';	
			 
		   $groupid = ( int ) $this->_request->getParam ( 'groups' );
		   $school_id = $this->_request->getParam ( 'school' ); 		
	       $classid  = $this->_request->getParam ( 'class' ); 
		   $cnname  = $this->_request->getParam ( 'cnname' ); 
		    
		   $this->view->groups=$groupid; 
		   $this->view->school_id=$school_id;  
		   $this->view->classid=$classid; 
		   $this->view->cnname=$cnname; 
		  
		  if($school_id){ 
			  $schoolarray=explode('_',$school_id);  
			  $schoolid=$schoolarray['1'];
		  }
		   
		 if($groupid){
			  $where .=' and  groupid="'.$groupid.'" ';
			 $this->view->groupid= $groupid; 
			 }
			 
		 if($schoolid){
			$where .=' and  schoolid="'.$schoolid.'" '; 
			$this->view->schoolid= $school_id; 
			} 
			
		 if($classid){
			$where .=' and  classid="'.$classid.'" ';
			  $this->view->classid= $classid; 
			 }	
	 
		 if($cnname){
			$where.=' and cnname="'.trim($cnname).'" ';
			$this->view->cnname= $cnname; 				 
			}else{
			 //throw new Exception ( '请输入您所要查询的孩子名称' );	 
			} 
			  
			$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			  $this->view->pageNum= $curpage;			
					
    			$offset = ($curpage - 1) * $count; 
			    $sql="select SQL_CALC_FOUND_ROWS s.cnname,p.ordertype,p.paynum,p.price,p.discount,p.totalprice,p.trueprice,p.addtime,p.effecttime,p.endtime,p.orderid  from  pay_order p left join student_unit_v s   on  s.uid_student = p.uid_student     ".$where."  order by p.addtime DESC limit $offset,$count";
				    //echo $sql;  exit;
			    $result = $this->_dbAdapter->fetchAll ($sql);  
			    
		    	$this->view->result= $result;
			  
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    			$page->setvar ( array ('groups' => $groupid,'school'=>$schoolid,'class'=>$classid) );
				$this->view->page= $page->getNumPage ();
	    
	   
	   
	    } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	  
	  
	   public function orderdetailAction() {
			try { 
		    
			$orderid =   $this->_request->getParam ( 'orderid' );  
			if(empty($orderid)){
			   throw new Exception ( '参数错误！' ); 
				}
			
			
			$name =   $this->_request->getParam ( 'name' ); 
			
		    $sql="select * from  pay_order where orderid=".$orderid;
		    $result = $this->_dbAdapter->fetchRow ($sql);   
		 
		   
		    $this->view->result=$result;  
			$this->view->name=$name; 
       
			
			
		} catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		}
		
	//优惠券  coupon 
	
	   public function couponAction() {
			try {   
			 
			
		} catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		}
	
	
	
	
	 public function opcouponAction() { 
		try {  
		 
		 if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
	    $p =$this->_request->getPost ();  
		  
	     
		$options=implode(',',$p['options']) ;
		
	    $code=$this->fast_uuid(); 
		$t=time();
		//插入规则表 
		$code=array(
		       'title'=>$p['description'],
		       'product_type'=>$options, 
			   'c_type'=>$p['optionsRadios'],
			   'buy_price'=>$p['buy_price'],
		       'expiry'=>$p['expiry'],
			   'money_max'=>$p['money_max'],
			   'money_reduce'=>$p['money_reduce'],  
			   'receive_limit'=>$p['receive_limit'],
			   'addtime'=>$t,
			   'amount'=>$p['amount'], 
		     ); 
		
		if($p['price']){
			$code['price']=$p['price'];
			}
		if($p['discount']){
			$code['discount']=$p['discount'];
			}
		
		$code_codes = $this->_dbAdapter->insert('coupon_code',$code);   
		$lastIId = $this->_dbAdapter->lastInsertId ();
		//在生成对应数量的优惠券
		
		 if($lastIId){ 
			  for($i=0;$i<$p['amount'];$i++){
				  $codes=array(
			        'c_id'=>$lastIId,
				    'code'=>$this->fast_uuid(), 
			      );  
				  
				  $result = $this->_dbAdapter->insert('coupon_code_codes',$codes);   	
				 }
			if($result){
			  $this->feedback ( '添加成功', '注意', '/'.$this->controller.'/coupon/', 'tip',0 ); 
			}
			 	    
		  } 
		   
		} catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
   
 //生成产品
   public function addproductAction() { 
		try {   
		
         } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
    
    public function opproductAction() { 
		try {  
		  //生成商品 
        if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}			
	    $p =$this->_request->getPost ();  
	    
		 
		 $data=array();
		foreach($p as  $key =>$value){ 
		 	$data[$key]=$value; 
			if($key=='starttime' || $key=='endtime'){
				$data[$key]=strtotime($value); 
				}
		 } 
		 
       $result = $this->_dbAdapter->insert('pay_product',$data);   
       if($result){
		   $this->feedback ( '添加成功', '注意', '/'.$this->controller.'/addproduct/', 'tip',0 );   
		   }
	    
         } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
      
        
     public function productlistAction() { 
		try {  
          
		  $t=time(); 
		  $sql="select * from pay_product"; 
		  $result = $this->_dbAdapter->fetchAll($sql);   
		   
		 foreach($result as $key =>$value){
			 if($value['endtime'] < $t){
				  $result[$key]['endtime'] ='已过期';
				 }else{
				 $result[$key]['endtime'] =date('Y-m-d H:i:s',$value['endtime']);
				 } 
			 }  
		   
          $this->view->result=$result;  
       } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
    private function fast_uuid($suffix_len=3){
        //! 计算种子数的开始时间
        static $being_timestamp = 1216681180;// 2012-5-10
       
        $time = explode(' ', microtime());
        $id = ($time[1] - $being_timestamp) . sprintf('%06u', substr($time[0], 2, 6));
        if ($suffix_len > 0)
        {
            $id .= substr(sprintf('%010u', mt_rand()), 0, $suffix_len);
        }
        return $id;
    }
   
  /*
  操作处理 :充值、消费、购买服务
  */ 
    public function operationAction() { 
		try {  
          //获取参数
		   $uid = ( int ) $this->_request->getParam ( 'uid' );
		   $pid = $this->_request->getParam ( 'pid' ); 		
	       $keyworld  = $this->_request->getParam ( 'keyworld' ); 
		    
		   $this->view->uid=$uid; 
		   $this->view->pid=$pid;  
		   $this->view->keyworld=$keyworld; 
		   $sql='select SQL_CALC_FOUND_ROWS s.*,p.*  from   pay_account p  left join  student s on  s.uid = p.uid_student where  p.uid_student='.$uid.' order by p.addtime DESC ';
			  
		  $result = $this->_dbAdapter->fetchAll ($sql);  
	       
		   foreach($result as $key => $value){
				 if($value['paytype']=='add'){
					$amount[$value['uid_student']]['balance']+=$value['amount'];  
					
					$amount[$value['uid_student']]['add']+=$value['amount'];
					
				  }else if($value['paytype']=='cost' or $value['pay_type']=='refund'){
					  
					$amount[$value['uid_student']]['balance']-=$value['amount'];
					 
					$amount[$value['uid_student']]['cost']+=$value['amount']; 
					
				  } 
		  } 
		  
		  $this->view->result= $result; 
		  $this->view->amount= $amount;  
		  
		   //查询所有未过期的产品
			$time=time();
			$sql="select * from pay_product where  endtime>'".$time."' "; 
			$productAll = $this->_dbAdapter->fetchAll ($sql);  
			 
			$this->view->productAll=$productAll;  
			 
		   
		   
		   
		   
          } catch ( Exception $e ) {
				$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} 
  /*
  操作处理end
  */	 
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

